package de.ars.client.animator;

public class AnimatedPoint extends Point
{
    private Point start;
    private Point target;
    private boolean animationInProgress = false;
    private float animationProcess;
    private float animationSpeed = 1.5f; 

    public AnimatedPoint( Point p )
    {
        start = new Point( );
        target = new Point( );
        setPointTo(p.x,p.y);
    }

    public AnimatedPoint( float x, float y )
    {
        this.x = x;
        this.y = y;
        start = new Point( x, y );
        target = new Point( x, y );
    }

    public void resetPosition(boolean setToTarget)
    {
        if (setToTarget)
        {
            this.setBoth( target );
            start.setBoth( target );
        }
        else
        {
            this.setBoth( start );
            target.setBoth( start );
        }
    }

    public Point getPoint()
    {
        return new Point( x, y );
    }

    public void startAnimationShrink( float factor,float animaionSpeed )
    {
        startAnimationTo( this.x * factor, this.y * factor, animationSpeed);
    }

    public void startAnimationTo( float x, float y,float animationSpeed )
    {
        this.animationSpeed = animationSpeed;
        this.animationProcess = 0.0f;
        this.animationInProgress = true;
        this.target.setX( x );
        this.target.setY( y );
    }

    public void process( long elapsed )
    {
        if( animationInProgress )
        {
            this.animationProcess += animationSpeed * elapsed / 1000;
            if( this.animationProcess > 1.0 )
            {
                animationInProgress = false;
            }
            else
            {
                this.setX( start.getX() - (start.getX() - target.getX()) * animationProcess );
                this.setY( start.getY() - (start.getY() - target.getY()) * animationProcess );
            }
        }
    }

    public float getStartX()
    {
        return this.start.getX();
    }

    public float getStartY()
    {
        return this.start.getY();
    }

    public float getTargetX()
    {
        return this.target.getX();
    }

    public float getTargetY()
    {
        return this.target.getY();
    }

    public void setAnimationInProgress( boolean state )
    {
        this.animationInProgress = state;
    }

    public boolean isAnimationInProgress()
    {
        return this.animationInProgress;
    }
    
    public void setPointTo(float x, float y)
    {
        this.setBoth( x, y );
        start.setBoth( x, y );
        target.setBoth( x, y );
    }
}
